// pages/taskHall.js
var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 页面配置
     */
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    orderData: [],
    marginTopPx: '0px',
    click_count: 0,
    doingList: [],
    taskList: [],
    turnList: [],
    finishList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取系统信息
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    var query = wx.createSelectorQuery();
    query.select('.swiper-tab').boundingClientRect(rect => {
      var height = rect.height + 'px';
      that.setData({
        marginTopPx: height
      })
    }).exec();

    this.callByTab();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.loadData();
  },

  loadData: function(){
    var that = this;
    var url = api.GetToDoOrderSwichList + '/' + app.globalData.userInfo.token;
    util.myRequest(url, false, function (data) {
      var doingList = that.setPhone(data.data.doing);
      var taskList = that.setPhone(data.data.task);
      var finishList = that.setPhone(data.data.finish);
      var turnList = that.setPhone(data.data.turn);

      // console.log(infoList)
      that.setData({
        doingList: doingList,
        taskList: taskList,
        finishList: finishList,
        turnList: turnList,
      });
    });
  },

  setPhone: function (infoList) {
    for (var i = 0; i < infoList.length; i++) {
      if (infoList[i].aoType == 'shop') {
        infoList[i].aoTakeInfo = JSON.parse(infoList[i].aoTakeInfo);
        if (infoList[i].aoTakeInfo.cellphone) {
          infoList[i].aoTakeInfo.cellphone = infoList[i].aoTakeInfo.cellphone.substr(0, 3) + '****' + infoList[i].aoTakeInfo.cellphone.substr(7)
        }
      }
    }
    return infoList;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 滑动切换tab
   */
  bindChange: function (e) {
    var that = this;
    this.callByTab(e.detail.current);
    that.setData({
      currentTab: e.detail.current
    });
  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      // this.callByTab(e.target.dataset.current);
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  callByTab: function (currentTab = 0) {
    // if (currentTab == 0) {
    //   this.getDataByToDo();
    //   return;
    // }
    // if (currentTab == 2) {
    //   this.getDataByDone();
    //   return;
    // }
  },
  getDataByToDo: function () {
    var that = this;
    var url = api.ToDoOrderList + '/' + app.globalData.userInfo.token;
    util.myRequest(url, false, function (data) {
      var infoList = data.data;
      for (var i = 0; i < infoList.length; i++) {
        if (infoList[i].aoType == 'shop') {
          infoList[i].aoTakeInfo = JSON.parse(infoList[i].aoTakeInfo);
          if (infoList[i].aoTakeInfo.cellphone) {
            infoList[i].aoTakeInfo.cellphone = infoList[i].aoTakeInfo.cellphone.substr(0, 3) + '****' + infoList[i].aoTakeInfo.cellphone.substr(7)
          }
        }
      }
      // console.log(infoList)
      that.setData({
        orderData: infoList
      });
    });
  },
  getDataByDone: function () {
    var that = this;
    var url = api.OrderHistory + '/' + app.globalData.userInfo.token;
    util.myRequest(url, false, function (data) {
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].aoTakeInfo) {
          data.data[i].aoTakeInfo = JSON.parse(data.data[i].aoTakeInfo)
          data.data[i].isUseTakeInfo = true
          console.log(data.data[i].aoTakeInfo)
        } else {
          data.data[i].isUseTakeInfo = true
        }
      }
      // console.log(data.data)
      that.setData({
        orderData: data.data
      });
    });
  },
  // 接单
  acceptOrder: function (e) {
    var aoId = e.target.id;
    var status = e.target.dataset.status;
    if (status >= 5) {
      return
    }
    let that = this
    let msg = "确定要进行该操作吗？"
    if (status == 1) {
      msg = '确定要接单吗？'
    } else if (status == 2) {
      msg = '确定要转单吗？'
      // if (that.data.click_count <= 3) {
      //   wx.showToast({
      //     title: '请扫该订单小票的二维码取货',
      //     icon: 'none',
      //     duration: 2000,
      //     mask: true
      //   })
      //   return
      // }
    } else if (status == 3) {
      msg = '确定配送完成吗？'
    } else if (status == 4) {
      msg = '确定评价吗？'
    } else if (status == 5) {
      msg = '已评价，订单完成了'
    }
    wx.showModal({
      title: '提示',
      content: msg,
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          that.changeStatus(aoId, status);
          if (status == 1){
            that.setData({
              currentTab: 2
            });
          }else{
            that.setData({
              currentTab: 0
            });
          }
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  // 转单
  turnOrder: function(e){
    var aoId = e.target.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要转单吗',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          that.turnOrderReq(aoId)
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 转单请求
  turnOrderReq: function (aoId) {
    var that = this;
    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.aoId = aoId;
    util.myRequest(api.AcceptTurnOrder, postData, function (data) {
      that.loadData();
      wx.showToast({
        title: '接收成功！',
        icon: 'success',
        duration: 2000
      })
    });
  },

  // 更改订单状态
  changeStatus: function (aoId, status) {
    var that = this;
    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.aoId = aoId;
    postData.status = status;
    console.log(postData);
    util.myRequest(api.knightChangeOrderStatus, postData, function (data) {
      that.loadData();
    });
  },
  // 到详情页
  goDetail: function (e) {
    wx.navigateTo({
      url: './../detail/detail?aoId=' + e.target.id
    })
  },

  // 到上传页
  goUpload: function (e) {
    wx.navigateTo({
      url: './../uploadImg/uploadImg?id=' + e.target.id
    })
  },
})